package com.zhangtai.modules.service.impl.salary;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.SalaryDistributionRecordingDao;
import com.zhangtai.modules.dto.EmployeeSalaryGroupDto;
import com.zhangtai.modules.entity.salary.SalaryDistributionRecordingEntity;
import com.zhangtai.modules.service.salary.SalaryDistributionRecordingService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
public class SalaryDistributionRecordingServiceImpl extends ServiceImpl<SalaryDistributionRecordingDao, SalaryDistributionRecordingEntity> implements SalaryDistributionRecordingService {

    @Autowired
    private SalaryDistributionRecordingDao salaryDistributionRecordingDao;
    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<SalaryDistributionRecordingEntity> page = this.page(
                new Page<SalaryDistributionRecordingEntity>(pageNum,pageSize),
                new QueryWrapper<SalaryDistributionRecordingEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public List<Map<String,Object>> queryPageList(
            @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
            @RequestParam(value = "orgId", required = false) List<Long> orgId,
            @RequestParam(value = "unitIds", required = false) List<Long> unitIds,
            @RequestParam(value = "jobIds", required = false) List<Long> jobIds,
            @RequestParam(value = "distributionTime", required = false) String distributionTime,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {


        Page<EmployeeSalaryGroupDto> page = new Page<EmployeeSalaryGroupDto>(pageNum,pageSize);
        List<Map<String,Object>> list = salaryDistributionRecordingDao.getSalaryDistributionRecordingInfoList(
                orgIds,orgId,unitIds,jobIds,distributionTime,
                StringUtils.isEmpty(name)?null:name,
                StringUtils.isEmpty(code)?null:code,
                page);
        return list;
    }
    @Override
    public SalaryDistributionRecordingEntity getByIdE(Long id) {
            SalaryDistributionRecordingEntity one = this.getOne(new QueryWrapper<SalaryDistributionRecordingEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(SalaryDistributionRecordingEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(SalaryDistributionRecordingEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            SalaryDistributionRecordingEntity entity = new SalaryDistributionRecordingEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}